package com.zcg.blogger.service.impl;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.aop.ThrowsAdvice;
import org.springframework.stereotype.Service;

import com.zcg.blogger.dao.ArticleDao;
import com.zcg.blogger.dao.CommentDao;
import com.zcg.blogger.dao.ConfigDao;
import com.zcg.blogger.dao.MessageDao;
import com.zcg.blogger.dao.ProposeDao;
import com.zcg.blogger.dao.UserDao;
import com.zcg.blogger.exception.UserException;
import com.zcg.blogger.model.Article;
import com.zcg.blogger.model.User;
import com.zcg.blogger.pojo.Pager;
import com.zcg.blogger.service.UserService;
import com.zcg.blogger.util.EmailUtil;
import com.zcg.blogger.util.MD5Util;
import com.zcg.blogger.util.RandomUtil;

@Service("userService")
public class UserServiceImpl implements UserService {

	private Map map = new HashMap();

	@Resource
	private UserDao userDao;

	@Resource
	private ConfigDao configDao;

	@Resource
	private ArticleDao articleDao;

	@Resource
	private CommentDao commentDao;

	@Resource
	private MessageDao messageDao;

	@Resource
	private ProposeDao proposeDao;

	public User getUserById(int userId) {
		return this.userDao.findUserById(userId);
	}

	public void add(User user) {
		user.setAvatar("resources/images/avatar/tou.jpg");
		this.userDao.insert(user);
	}

	public void update(User user) {
		this.userDao.update(user);
	}

	public void delete(User user) {
		int i = this.userDao.delete(user.getId());
	}

	public User loadById(int id) {
		return this.userDao.findUserById(id);
	}

	public boolean loadByName(String username) {
		return this.userDao.checkName(username);
	}

	public List<User> loadUsers() {
		return this.userDao.selectAll();
	}

	public User check(String username, String password) {
		map.put("username", username);
		map.put("password", password);
		return this.userDao.check(map);
	}

	public void sendEmail(String email) throws Exception {
		map.clear();
		map.put("to", email);
		map.put("subject", "张晨刚博客 找回密码");
		map.put("text",
				configDao.getAttribute("findPassword").replaceAll(
						"updatePassword",
						"updatePassword/" + MD5Util.MD5(email)));
		EmailUtil.toEmail(map);
	}

	public void updatePassword(String email, String password) {
		map.put("email", email);
		map.put("password", password);
		userDao.updatePasswordByEmail(map);
	}

	@Override
	public List<Object> loadObjectsByOperate(int id, int operate) {
		List<Object> os = null;
		map.clear();
		map.put("id", id);
		map.put("pageOffset", Pager.getPager().getPageOffset());
		map.put("pageSize", Pager.getPager().getPageSize());
		switch (operate) {
		case 0:
			os = new ArrayList();
			List<Object> os1 = articleDao.findNewestArticleByUserId(map);
			List<Object> os2 = commentDao.findNewestCommentByUserId(map);
			List<Object> os3 = messageDao.findNewestMessageByUserId(map);
			List<Object> os4 = proposeDao.findNewestProposeByUserId(map);
			os.add(os1);
			os.add(os2);
			os.add(os3);
			os.add(os4);
			break;
		case 1:
			os = articleDao.findArticleByUserId(map);
			break;
		case 2:
			os = commentDao.findCommentByUserId(map);
			break;
		case 3:
			os = messageDao.findMessageByUserId(map);
			break;
		case 4:
			os = proposeDao.findProposeByUserId(map);
			break;
		default:
			break;
		}
		return os;
	}

	@Override
	public void sendCheckCode(String email) throws Exception {

		map.clear();
		map.put("to", email);
		map.put("subject", "张晨刚博客 验证码");
		map.put("text",
				configDao.getAttribute("checkCode").replaceAll(
						"<b>5678<b/>",
						"<b>"+RandomUtil.checkCode()+"<b/>"));
		EmailUtil.toEmail(map);
		
	}

	@Override
	public void updateEmail(String email, String checkCode, int id) {
		if (checkCode != "1234") {
			throw new UserException("验证码不正确");
		}
		map.clear();
		map.put("id", id);
		map.put("email", email);
		userDao.updateEmailByEmail(map);

	}

	@Override
	public void updatePassword(String oldPassword, String newPassword, int id) {
		map.clear();
		map.put("oldPassword", MD5Util.MD5(oldPassword));
		map.put("newPassword", newPassword);
		map.put("id", id);
		userDao.updatePasswordByPassword(map);

	}

	@Override
	public void updateAvatar(String avatar, int id) {
		map.clear();
		map.put("avatar", avatar);
		map.put("id", id);
		userDao.updateAvatarById(map);

	}

}
